define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'vppz/community/index' + location.search,
                    add_url: 'vppz/community/add',
                    edit_url: 'vppz/community/edit',
                    editpassword_url: 'vppz/community/editpassword',
                    medical_staff_url: 'vppz/medical_staff/index',
                    del_url: 'vppz/community/del',
                    table: 'vppz_community',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'weigh',
                fixedColumns: true,
                fixedRightNumber: 1,
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id'),searchable:false},
                        {field: 'area.id', title:'所属运营区', operate: 'LIKE',addClass:'selectpage',extend:'data-source="vppz/area/index"',visible: false},
						{field: 'area.name', title:'所属运营区', operate: 'LIKE',searchable:false},
                        {field: 'name', title: '社区名称', operate: 'LIKE'},
                        {field: 'type_text', title: '社区类型', operate: 'LIKE',searchable:false},
                        {field: 'contact_phone', title: '联系方式', operate: 'LIKE'},
                        {field: 'cover_image', title: '宣传图', operate: 'LIKE', events: Table.api.events.image, formatter: Table.api.formatter.image,searchable:false},

                        {field: 'province', title: '所在省份', operate: 'LIKE'},
                        {field: 'city', title: '所在城市', operate: 'LIKE'},
                        {field: 'district', title: '所在区县', operate: 'LIKE'},
                        {field: 'address', title: '详细地址', operate: 'LIKE',searchable:false},
                        {field: 'status', title: __('Status_name'), searchList: {"open":__('Status_name open'),"closed":__('Status_name closed'),"maintenance":__('Status_name maintenance')}, formatter: Table.api.formatter.normal},

                        {field: 'admin_mobile', title: '管理员账号', operate:'BETWEEN',searchable:false},
                        {field: 'createtime', title: __('Createtime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'updatetime', title: __('Updatetime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        //{field: 'admin_id', title: __('Admin_id')},
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate,
                            buttons: [{
                                    name: 'staff',
                                    title: '医师管理',
									text: '医师管理',
                                    classname: 'btn btn-xs btn-success btn-dialog',
                                    icon: 'fa fa-list',
                                    url: 'vppz/medical_staff/index'
                                },{
                                    name: 'package',
                                    title: '套餐管理',
									text: '套餐管理',
                                    classname: 'btn btn-xs btn-success btn-dialog',
                                    icon: 'fa fa-list',
                                    url: 'vppz/health_package/index',
                                },{
                                    name: 'password',
                                    title: '管理员密码',
									text: '管理员密码',
                                    classname: 'btn btn-xs btn-success btn-dialog',
                                    icon: 'fa fa-list',
                                    url: 'vppz/community/editpassword'
                                }],
                                formatter: function (value, row, index) { //隐藏自定义的视频按钮
                                    var that = $.extend({}, this);
                                    var table = $(that.table).clone(true);

                                    //权限判断
                                   if(row.show_yishi === false){ //通过Config.chapter 获取后台存的chapter
                                        $(table).data("operate-staff", null);
                                    }
                                    if(row.show_taocan === false){
                                        console.log('##########################');
                                        $(table).data("operate-package", null);
                                    }
                                        that.table = table;
                                    return Table.api.formatter.operate.call(that, value, row, index);
                                }
						}
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        recyclebin: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    'dragsort_url': ''
                }
            });

            var table = $("#table");

            // 为表格绑定事件
            Table.api.bindevent(table);
        },

        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        editpassword: function () {
            Controller.api.bindevent();
        },
		services: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});
